package controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.event.AjaxBehaviorEvent;

import pojo.Dotuoidoanhthu;
import pojo.Loainguoidungdoanhthu;
import pojo.Loaisanpham;
import pojo.Sanpham;
import pojo.Sanphamdoanhthu;
import util.BeanUtil;
import util.MessageNotificationHepler;
import dao.ChitiethoadonDAO;
import dao.HoadonDAO;
import dao.LoaisanphamDAO;
import dao.SanphamdoanhthuDAO;

@ManagedBean(name = "thongKeBean")
@ViewScoped
public class ThongKeBean implements Serializable {

	private List<Loaisanpham> lstLoaiSanPham = ((HeaderBean) BeanUtil
			.findBean("headerBean")).getLstLoaiSanPham();

	private static final long serialVersionUID = 1L;
	private int nTop = 10;
	private int nThoigian = -1;
	private List<Sanpham> lstSanPham;// = new ArrayList<Sanpham>();

	private String doiTuongThongKe = "";
	private int maNhomThongKe;
	private int maLoaiThongKe;
	private String loaiNguoiDung;
	private String doTuoi;
	private String loaiThoiGian;
	private int khoangThoiGian;
	private String thoiGianTu;
	private String thoiGianDen;

	@PostConstruct
	public void init() {
		lstSanPham = new ArrayList<Sanpham>();
	}

	public void LietKeSanPhamMoiNhat() {
		ChitiethoadonDAO cthddao = new ChitiethoadonDAO();
		System.out.println(getnTop() + " --------New------- ");
		lstSanPham = cthddao.GetTopNSanPhamMoiNhat(getnTop(), getnThoigian());
		MessageNotificationHepler.ShowMessage(String.format(
				"Liệt kê '%s' sản phẩm mới nhất thành công!", getnTop()));
	}

	public void LietKeSanPhamBanChayNhat() {
		ChitiethoadonDAO cthddao = new ChitiethoadonDAO();
		System.out.println(getnTop() + " --------Sell------- ");
		lstSanPham = cthddao.GetTopNSanPhamBanChayNhat(getnTop(),
				getnThoigian());
		MessageNotificationHepler.ShowMessage(String.format(
				"Liệt kê '%s' sản phẩm bán chạy nhất thành công!", getnTop()));
	}

	/*
	 * public void LietKeTheoNhuCau() {
	 * 
	 * System.out.println("Liet Ke Nhu Cau: " + getMaNhomLietKe() + " -- " +
	 * getMaLoaiLietKe() + " -- " + getTrangThaiDuyet() + " -- " +
	 * getTrangThaiXoa());
	 * 
	 * BinhluanDAO bldao = new BinhluanDAO();
	 * 
	 * if (-1 == getMaNhomLietKe())
	 * setLstBinhLuan(bldao.GetBinhLuanTheoMaLoaiVaTrangThai( getMaNhomLietKe(),
	 * getTrangThaiDuyet(), getTrangThaiXoa())); else
	 * setLstBinhLuan(bldao.GetBinhLuanTheoMaLoaiVaTrangThai( getMaLoaiLietKe(),
	 * getTrangThaiDuyet(), getTrangThaiXoa()));
	 * 
	 * setLstBinhLuan(bldao.removeDuplicate(getLstBinhLuan())); // Chưa liệt kê:
	 * }
	 */

	public void changeNhomListener(AjaxBehaviorEvent event) {
		System.out.println("listener: called by "
				+ event.getComponent().getClass().getName());

		HtmlSelectOneMenu combobox = (HtmlSelectOneMenu) event.getComponent();
		int contentValue = (int) combobox.getValue();

		System.out.println(combobox.getId() + " -- " + contentValue);

		setMaNhomThongKe(contentValue);
		ThayDoiGiaTriLoaiSanPham();
	}

	public void ThayDoiGiaTriLoaiSanPham() {
		System.out.println("Thay doi gia tri Loai SP: " + getMaNhomThongKe());
		if (-1 == maNhomThongKe)
			lstLoaiSanPham = ((HeaderBean) BeanUtil.findBean("headerBean"))
					.getLstLoaiSanPham();
		else {
			lstLoaiSanPham = (new LoaisanphamDAO())
					.GetLoaiSanPhamTheoMaNhom(maNhomThongKe, false);
		}
	}

	// Thống kê
	public void ThongKe() {
		System.out.println("Thong Ke: " + getDoiTuongThongKe());

		try {
			
			if ("" != doiTuongThongKe || "" != loaiThoiGian) {
				if (doiTuongThongKe.equalsIgnoreCase("SanPham"))
					ThongKeTheoSanPham();
				else if (doiTuongThongKe.equalsIgnoreCase("NguoiDung"))
					ThongKeTheoNguoiDung();
				if (doiTuongThongKe.equalsIgnoreCase("DoTuoi"))
					ThongKeTheoDoTuoi();
				MessageNotificationHepler.ShowMessage("Thống Kê Hoàn Tất..!");
			}

		} catch (Exception ex) {
			MessageNotificationHepler
					.ShowMessage("Có Lỗi trong việc thực hiện thao tác!");
			return;
		}
	}

	private List<Sanphamdoanhthu> lstSanPhamDoanhThu;

	private void ThongKeTheoSanPham() {

		System.out.println("Liet Ke Nhu Cau: " + getMaNhomThongKe() + " -- "
				+ getMaLoaiThongKe() + " -- " + getKhoangThoiGian() + " -- "
				+ getThoiGianTu() + " -- " + getThoiGianDen());

		ChitiethoadonDAO cthddao = new ChitiethoadonDAO();
		List<Object[]> lstResult = cthddao.GetKetQuaThongKeDoanhThuTheoSanPham(
				maNhomThongKe, maLoaiThongKe, getLoaiThoiGian(),
				getKhoangThoiGian(), getThoiGianTu(), getThoiGianDen());
		setLstSanPhamDoanhThu(SanphamdoanhthuDAO
				.ConvertToListSanPhamDoanhThu(lstResult));
		System.out.println("Ket qua thong Ke Doanh Thu Sanpham: "
				+ getLstSanPhamDoanhThu().size());
	}

	public long TinhTongTienTheoSanPham() {
		long tongTien = 0;
		for (int i = 0; i < lstSanPhamDoanhThu.size(); i++) {
			tongTien = tongTien + lstSanPhamDoanhThu.get(i).getTongtien();
		}
		return tongTien;
	}

	private List<Loainguoidungdoanhthu> lstLoaiNguoiDungDoanhThu;

	private void ThongKeTheoNguoiDung() {

		System.out.println("Liet Ke Nhu Cau: " + getLoaiNguoiDung() + " -- "
				+ getKhoangThoiGian() + " -- " + getThoiGianTu() + " -- "
				+ getThoiGianDen());

		HoadonDAO hddao = new HoadonDAO();
		setLstLoaiNguoiDungDoanhThu(hddao
				.GetKetQuaThongKeDoanhThuTheoNguoiDung(getLoaiNguoiDung(),
						getLoaiThoiGian(), getKhoangThoiGian(),
						getThoiGianTu(), getThoiGianDen()));

		System.out.println("Ket qua thong Ke Doanh Thu Nguoi Dung: "
				+ getLstLoaiNguoiDungDoanhThu().size());
	}

	public long TinhTongTienTheoNguoiDung() {
		long tongTien = 0;
		for (int i = 0; i < getLstLoaiNguoiDungDoanhThu().size(); i++) {
			tongTien = tongTien
					+ getLstLoaiNguoiDungDoanhThu().get(i).getTongdoanhthu();
		}
		return tongTien;
	}

	private List<Dotuoidoanhthu> lstDoTuoiDoanhThu;

	private void ThongKeTheoDoTuoi() {

		System.out.println("Liet Ke Nhu Cau: " + getDoTuoi() + " -- "
				+ getKhoangThoiGian() + " -- " + getThoiGianTu() + " -- "
				+ getThoiGianDen());

		HoadonDAO hddao = new HoadonDAO();
		setLstDoTuoiDoanhThu(hddao.GetKetQuaThongKeDoanhThuTheoDoTuoi(
				getDoTuoi(), getLoaiThoiGian(), getKhoangThoiGian(),
				getThoiGianTu(), getThoiGianDen()));

		System.out.println("Ket qua thong Ke Doanh Thu Do Tuoi: "
				+ getLstDoTuoiDoanhThu().size());
	}

	public long TinhTongTienTheoDoTuoi() {
		long tongTien = 0;
		for (int i = 0; i < getLstDoTuoiDoanhThu().size(); i++) {
			tongTien = tongTien
					+ getLstDoTuoiDoanhThu().get(i).getTongdoanhthu();
		}
		return tongTien;
	}

	// -------------------------
	public List<Sanpham> getLstSanPham() {
		return lstSanPham;
	}

	public void setLstSanPham(List<Sanpham> lstSanPham) {
		this.lstSanPham = lstSanPham;
	}

	public String getDoiTuongThongKe() {
		return doiTuongThongKe;
	}

	public void setDoiTuongThongKe(String doiTuongThongKe) {
		this.doiTuongThongKe = doiTuongThongKe;
	}

	public String getLoaiThoiGian() {
		return loaiThoiGian;
	}

	public void setLoaiThoiGian(String loaiThoiGian) {
		this.loaiThoiGian = loaiThoiGian;
	}

	public int getMaNhomThongKe() {
		return maNhomThongKe;
	}

	public void setMaNhomThongKe(int maNhomThongKe) {
		this.maNhomThongKe = maNhomThongKe;
	}

	public int getMaLoaiThongKe() {
		return maLoaiThongKe;
	}

	public void setMaLoaiThongKe(int maLoaiThongKe) {
		this.maLoaiThongKe = maLoaiThongKe;
	}

	public String getLoaiNguoiDung() {
		return loaiNguoiDung;
	}

	public void setLoaiNguoiDung(String loaiNguoiDung) {
		this.loaiNguoiDung = loaiNguoiDung;
	}

	public String getDoTuoi() {
		return doTuoi;
	}

	public void setDoTuoi(String doTuoi) {
		this.doTuoi = doTuoi;
	}

	public List<Loaisanpham> getLstLoaiSanPham() {
		return lstLoaiSanPham;
	}

	public void setLstLoaiSanPham(List<Loaisanpham> lstLoaiSanPham) {
		this.lstLoaiSanPham = lstLoaiSanPham;
	}

	public int getnTop() {
		return nTop;
	}

	public void setnTop(int nTop) {
		this.nTop = nTop;
	}

	public int getnThoigian() {
		return nThoigian;
	}

	public void setnThoigian(int nThoigian) {
		this.nThoigian = nThoigian;
	}

	public int getKhoangThoiGian() {
		return khoangThoiGian;
	}

	public void setKhoangThoiGian(int khoangThoiGian) {
		this.khoangThoiGian = khoangThoiGian;
	}

	public String getThoiGianTu() {
		return thoiGianTu;
	}

	public void setThoiGianTu(String thoiGianTu) {
		this.thoiGianTu = thoiGianTu;
	}

	public String getThoiGianDen() {
		return thoiGianDen;
	}

	public void setThoiGianDen(String thoiGianDen) {
		this.thoiGianDen = thoiGianDen;
	}

	public List<Sanphamdoanhthu> getLstSanPhamDoanhThu() {
		return lstSanPhamDoanhThu;
	}

	public void setLstSanPhamDoanhThu(List<Sanphamdoanhthu> lstSanPhamDoanhThu) {
		this.lstSanPhamDoanhThu = lstSanPhamDoanhThu;
	}

	public List<Loainguoidungdoanhthu> getLstLoaiNguoiDungDoanhThu() {
		return lstLoaiNguoiDungDoanhThu;
	}

	public void setLstLoaiNguoiDungDoanhThu(
			List<Loainguoidungdoanhthu> lstLoaiNguoiDungDoanhThu) {
		this.lstLoaiNguoiDungDoanhThu = lstLoaiNguoiDungDoanhThu;
	}

	public List<Dotuoidoanhthu> getLstDoTuoiDoanhThu() {
		return lstDoTuoiDoanhThu;
	}

	public void setLstDoTuoiDoanhThu(List<Dotuoidoanhthu> lstDoTuoiDoanhThu) {
		this.lstDoTuoiDoanhThu = lstDoTuoiDoanhThu;
	}

}
